<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function isMobile(){
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        return true;

//此条摘自TPM智能切换模板引擎，适合TPM开发
    if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
        return true;
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
//找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
//判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
//从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
//协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
}
/*
 * 返回JSON格式
 */
function ret_json($data=''){
    $data = json_encode($data);
    echo $data;die;
}
/*
 * 获取网站头部
 */
function getCurlList($url=''){
    $cookie_file = dirname(__FILE__).'/cookie.txt';
//$cookie_file = tempnam("tmp","cookie");

//先获取cookies并保存
    $ch = curl_init($url); //初始化
    curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串，而非直接输出
    curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
    curl_exec($ch);
    curl_close($ch);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
/*
 * 抓取
 */
function getPregMatch($reg='',$result='',$type=1){
    if($type == 1){
        preg_match($reg, $result, $matchs);
    }else{
        preg_match_all($reg, $result, $matchs);
    }
    return $matchs;
}
/*
 * 防盗链 图片处理
 */
function http_down($url, $filename, $timeout = 60) {
    $path = dirname($filename);
    if (!is_dir($path) && !mkdir($path, 0755, true)) {
        return false;
    }
    $fp = fopen($filename, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    return $filename;
}
/*
 * 获取文件后缀
 */
function get_extension($file)
{
    return substr(strrchr($file, '.'), 1);
}
/*
 *获取域名
 */
function getInurl(){
    $request=request();
    $domain=$request->domain();
    return $domain;
}
/*
 *去除html
 */
function delHtmlIco($content){
    $content=preg_replace('/&nbsp;/is','',$content);
    $content=preg_replace("/<a[^>]*>/i","",$content);
    $content=preg_replace("/<\/a>/i","",$content);
    $content=preg_replace("/<div[^>]*>/i","",$content);
    $content=preg_replace("/<\/div>/i","",$content);
    $content=preg_replace("/<br[^>]*>/i","",$content);
    $content=preg_replace("/<\/br>/i","",$content);
    return $content;
    $content=preg_replace("/<!--[^>]*-->/i","",$content);//注释内容    
    $content=preg_replace("/style=.+?['|\"]/i",'',$content);//去除样式    
    $content=preg_replace("/class=.+?['|\"]/i",'',$content);//去除样式    
    $content=preg_replace("/id=.+?['|\"]/i",'',$content);//去除样式       
    $content=preg_replace("/lang=.+?['|\"]/i",'',$content);//去除样式        
    $content=preg_replace("/width=.+?['|\"]/i",'',$content);//去除样式     
    $content=preg_replace("/height=.+?['|\"]/i",'',$content);//去除样式     
    $content=preg_replace("/border=.+?['|\"]/i",'',$content);//去除样式     
    $content=preg_replace("/face=.+?['|\"]/i",'',$content);//去除样式     
    $content=preg_replace("/face=.+?['|\"]/",'',$content);//去除样式 只允许小写 正则匹配没有带 i 参数  
    return $content;
}